﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BestBuy.Common;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageBuyerManager : System.Web.UI.Page
    {
        private CBuyerOffer _cbtm = new CBuyerOffer();
        private static int _currentPage = 1;
        private static int _currentStatus = -2;

        protected void Page_Load(object sender, EventArgs e)
        {
            pnError.Visible = false;
            pnEmtry.Visible = false;
            if (!IsPostBack)
            {
                _currentStatus = -2;
                InitializationButton(false);
                Filter();
            }
        }

        private void LoadGV(int status)
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            int size = gridview.PageSize;
            int begin = (_currentPage - 1) * size;
            List<BangTinMua> list = _cbtm.GetList(status, begin, size);
            if (list.Count > 0)
            {
                gridview.DataSource = list;
                gridview.DataBind();
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;
            }
            else
            {
                if (_currentStatus == 0)
                {
                    InitializationButton(false);
                }
                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gridview.DataSource = null;
                gridview.DataBind();
                lbGridViewEmtry.Text = "There are no data.";

            }
        }

        private void InitializationButton(bool b)
        {
            lbtnActive.Visible = b;
            lbtnDelete.Visible = b;
        }

        protected void cbbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cbbStatus.SelectedValue.Equals("0"))
            {
                InitializationButton(false);
            }
            else
            {
                InitializationButton(true);
            }
            _currentStatus = Convert.ToInt32(cbbStatus.SelectedValue);
            Filter();
        }

        private void Filter()
        {
            _currentPage = 1;
            LoadTotalPageNumber(_currentStatus);
            LoadGV(_currentStatus);
        }

        protected void LoadTotalPageNumber(int status)
        {
            int soLuongTrang = _cbtm.slTrangDuLieu(gridview.PageSize, status);
            lbTrang.Text = soLuongTrang.ToString();
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            LoadGV(_currentStatus);
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                LoadGV(_currentStatus);
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                LoadGV(_currentStatus);
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            LoadGV(_currentStatus);
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            LoadGV(_currentStatus);
        }

        protected void lbtnActive_Click(object sender, EventArgs e)
        {
            lbError.Text = "";
            int size = gridview.Rows.Count;
            bool kq = false;
            int idbangtin;
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gridview.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        idbangtin = Convert.ToInt32(((Label)gridview.Rows[i].FindControl("lbID")).Text);
                        kq = _cbtm.UpdateStatus(idbangtin, (int)BangTinMuaStatus.Active);
                        if (!kq)
                        {
                            pnError.Visible = true;
                            pnError.CssClass = "InLeft error message";
                            lbError.Text = lbError.Text + "Error.This code : " + idbangtin.ToString() + " can not be active.Please try again <br/>";
                        }
                    }
                }
                catch
                {
                    Filter();
                }
            }
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text = "Active successfully";
            }
            Filter();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            lbError.Text = "";
            int size = gridview.Rows.Count;
            bool kq = false;
            int idbangtin;
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gridview.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        idbangtin = Convert.ToInt32(((Label)gridview.Rows[i].FindControl("lbID")).Text);
                        kq = _cbtm.UpdateStatus(idbangtin, (int)BangTinMuaStatus.Disapproved);
                        if (!kq)
                        {
                            pnError.Visible = true;
                            pnError.CssClass = "InLeft error message";
                            lbError.Text = lbError.Text + "Error.This code : " + idbangtin.ToString() + " can not be active.Please try again <br/>";
                        }
                    }
                }
                catch
                {
                    Filter();
                }
            }
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text = "Delete successfully";
            }
            Filter();
        }

        protected void lnkPopup_Click(object sender, EventArgs e)
        {
            LinkButton lnkPopup = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkPopup.NamingContainer;
            string id = gridview.DataKeys[row.RowIndex].Value.ToString();
            BangTinMua btm = _cbtm.GetBuyerOfferID(Convert.ToInt32(id));
            lbID.Text = id;
            lbSubject.Text = btm.TieuDe;
            lbContents.Text = btm.NoiDung;
            lbCreationDate.Text = btm.NgayDang;
            lbExpiryTime.Text = btm.ThoiHanDang.ToString() + " days";
            lbBuyingFrequency.Text = btm.TanSuatMuaHang.ToString();
            lbQuantityRequired.Text = btm.SoLuongMua.ToString() + " " + btm.DonVi.TenDonVi;
            lbAnnualPurchaseVolume.Text = btm.KhoiLuongMuaHangNam.ToString() + " " + btm.DonVi.TenDonVi;
            lbKeywords.Text = btm.TuKhoa;
            lbDescriptions.Text = btm.MoTa;
            if (btm.TinhTrang == 0)
            {
                lbtnPopupActive.Visible = true;
                lbtnPopupDelete.Visible = true;
            }
            else
            {
                lbtnPopupActive.Visible = false;
                lbtnPopupDelete.Visible = false;
            }
            mpeSanPham.Show();
        }


        public string CheckStatus(object obj)
        {
            int status = Convert.ToInt32(obj.ToString());
            string str = "";
            if (status == (int)BangTinMuaStatus.Unverified)
            {
                str = "Unverified";
            }
            else if (status == (int)BangTinMuaStatus.Active)
            {
                str = "Active";
            }
            else if (status == (int)BangTinMuaStatus.ExpiryTime)
            {
                str = "ExpiryTime";
            }
            else if (status == (int)BangTinMuaStatus.Deleted)
            {
                str = "Deleted";
            }
            else if (status == (int)BangTinMuaStatus.Disapproved)
            {
                str = "Disapproved";
            }
            return str;
        }

        protected void lbtnPopupActive_Click(object sender, EventArgs e)
        {
            try
            {
                int idbangtin = Convert.ToInt32(lbID.Text);
                bool kq = _cbtm.UpdateStatus(idbangtin, (int)BangTinMuaStatus.Active);
                if (!kq)
                {
                    pnError.Visible = true;
                    pnError.CssClass = "InLeft error message";
                    lbError.Text = lbError.Text + "Error.This code : " + idbangtin.ToString() + " can not be active.Please try again <br/>";
                }
                else
                {
                    pnError.Visible = true;
                    pnError.CssClass = "InLeft success message";
                    lbError.Text = "Active successfully";
                }
            }
            catch
            {
                Filter();
            }
            Filter();
        }

        protected void lbtnPopupDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int idbangtin = Convert.ToInt32(lbID.Text);
                bool kq = _cbtm.UpdateStatus(idbangtin, (int)BangTinMuaStatus.Disapproved);
                if (!kq)
                {
                    pnError.Visible = true;
                    pnError.CssClass = "InLeft error message";
                    lbError.Text = lbError.Text + "Error.This code : " + idbangtin.ToString() + " can not be active.Please try again <br/>";
                }
                else
                {
                    pnError.Visible = true;
                    pnError.CssClass = "InLeft success message";
                    lbError.Text = "Delete successfully";
                }
            }
            catch
            {
                Filter();
            }
            Filter();
        }

    }
}